#include "LED.h"
#include "tim.h"
#include "main.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <RS485.h>
#include "random.h"
#include "interface.h"

__IO u16 pwm_y=0;
__IO u16 pwm_w=0;

__IO  u16 pwm_w1=0;
__IO  u16 pwm_y1=0;
__IO  u16 pwm_w2=0;
__IO  u16 pwm_y2=0;
__IO  u16 pwm_w3=0;
__IO  u16 pwm_y3=0;
__IO  u16 pwm_w4=0;
__IO  u16 pwm_y4=0;
__IO  u8 eff_randno=0;

//------------------------------------------------------
#define 灭	0
#define LED_MIE	灭

u16 printYpwm=0;
u16 printWpwm=0;
//------------------------------------------------------
static u16 itimer=0;//作为全局变量，同步时调用
static u16 eff_count=0;
static u8  randNo=0;

static float effr_m = 0;
static float effr_n = 26;
static float effr_r = 2;

static float eff_ss_max = 35;
static float eff_ss_min = 0;
static float eff_ss_r = 2;

static float eff_Yfire_max = 100;
static float eff_Yfire_min = 00;
static float eff_Yfire_r = 8;


static float eff_Wfire_max = 5;
static float eff_Wfire_min = 2;
static float eff_Wfire_r = 1;

static u16 W_Cnt=0;
u16 Yeff_count=0;
u16 Weff_count=0;

u8 Yeff_flag=0;
u8 Weff_randn=0;

//------------------------------------------------------


void eff_paparazzi();



void eff_strobe(void);
void eff_shortout();
static void eff_fireW(void);
static void LEC_AllOff(void);
static void eff_fireY(void);
static void eff_fireY2(void);




const u16 Fire_tab[]=
{

73 ,151,232,182,326,135,239,40,176,370,238,331,72,311,397,192,292,265,158,312,78,142,86,357,174,264,295,183,94,352,235,187,159,184,229,74,39,242,215,1,
323,50 ,161,157,210,171,314,50,64,293,66,52,396,53,165,74,42,230,8,15,180,158,70,303,327,167,260,7,205,313,256,94,247,327,215,123,101,319,22,183,123,50,71,214,208,174,381,
223,205,332,100,235,49,56,281,7,370,49,117,379,83,302,39,264,233,354,383,274,322,13,108,142,139,138,71,320,82,195,298,343,7,114,252,153,40,
369,232,6,63,338,339,299,383,195,34,76,158,228,186,242,87,230,394,60,311,358,148,151,230,189,367,221,11,2,318,149,124,57,216,301,19,160,186,50,356,303,339,12,268,90,382,43,88,305,4,263,343,
380,258,220,304,295,242,307,41,349,140,134,360,285,379,256,205,30,362,356,250,396,225,223,325,143,244,332,334,141,98,229,79,97,319,190,232,279,241,133,295,158,120,348,286,145,211,127,221,224,155,229,78,248,153,149,193,273,187,302,130,180,159,175,124,295,322,331,244,305,52,248,260,69,68,83,72,324,87,164,381,141,369,315,147,170,
345,107,167,12,89,282,174,174,135,33,375,71,383,365,194,6,52,143,9,76,339,393,24,268,385,394,391,277,306,11,347,90,297,319,42,86,332,256,346,327,100,252,307,143,255,105,108,111,14,95,163,352,327,252,365,370,
229,57,259,114,60,203,289,326,353,272,90,20,25,222,16,33,374,132,265,387,231,8,96,83,375,334,223,24,199,103,234,227,233,321,55,388,328,350,277,16,320,339,214,136,259,293,240,64,177,63,183,53,148,117,
332,383,302,88,210,349,5,36,18,79,95,206,323,2,15,84,387,218,101,107,255,122,301,203,141,196,143,59,371,58,327,180,358,235,115,1,228,243,50,92,224,17,65,134,231,74,345,225,318,26,211,134,44,78,
28,281,189,191,137,227,23,236,318,378,159,367,20,178,6,8,201,222,58,48,332,303,6,338,110,156,230,20,79,114,327,350,302,370,219,229,50,243,20,365,51,159,314,190,147,270,219,74,313,168,265,371,43,4,278,112,89,214,195,133,75,
161,275,132,106,275,131,210,101,241,342,62,117,58,215,56,359,16,372,374,194,207,365,244,185,188,349,398,378,34,53,142,47,397,48,267,347,357,115,52,190,118,311,352,52,
146,294,222,11,203,59,357,219,41,16,87,184,281,199,156,136,33,173,160,143,359,100,62,284,381,282,194,352,121,376,251,109,23,164,3,221,172,308,225,171,322,331,292,3,350,211,93,182,9,111,49,290,306,218,39,371,247,93,121,42,269,163,316,387,101,70,198,274,305,211,50,31,112,128,123,161,385,58,221,183,139,144,165,269,239,62,
192,259,305,23,203,363,202,266,209,188,180,368,177,198,363,168,54,366,352,342,389,213,227,123,298,156,6,386,307,53,278,273,325,129,193,47,131,46,244,225,12,21,373,301,94,53,343,48,182,3,392,243,329,242,377,178,127,76,282,106,182,62,25,333,268,76,330,44,28,268,23,6,164,7,49,67,169,96,117,368,53,376,156,341,250,337,124,353,211,286,111,
320,92,65,225,201,332,115,58,379,136,308,150,255,263,188,375,321,218,34,42,160,48,200,24,173,101,106,58,330,237,72,53,343,16,263,64,154,289,91,184,260,395,369,148,124,223,22,341,360,88,244,160,279,86,115,125,13,204,122,389,305,348,88,86,272,103,46,256,396,218,30,179,235,75,3,31,258,80,35,192,175,98,47,10,157,51,277,
3,78,44,343,372,274,341,321,365,68,150,195,306,58,265,226,211,283,320,128,297,160,110,126,34,349,204,217,185,262,191,116,119,146,168,319,140,173,220,131,379,63,50,35,139,74,289,211,10,222,22,225,319,300,381,324,376,33,296,139,166,30,139,285,16,140,289,298,301,153,19,202,131,214,13,236,156,
164,213,120,166,83,168,94,116,240,164,238,345,183,45,114,8,216,140,272,301,356,304,336,294,145,5,298,235,155,41,307,328,280,170,391,206,199,47,117,215,179,397,170,243,328,206,122,377,265,104,131,255,361,343,121,203,311,64,67,236,351,312,69,256,
80,297,111,32,53,221,176,307,243,355,79,200,213,315,272,56,389,64,19,68,27,2,182,55,152,280,386,105,103,386,19,251,97,211,217,291,275,280,81,86,162,334,126,99,115,102,100,39,76,
111,376,201,22,207,324,98,181,83,335,291,213,11,314,267



};

const u16 Dim_tab[101]=
{

0   ,
#if 0

//s23
23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 ,  
44 , 45 , 46 , 47 , 48 , 50 , 52 , 54 , 57 , 59 , 61 , 64 , 66 , 69 , 72 , 75 , 77 , 80 , 83 , 87 , 90 , 
93 , 96 , 100 ,103 ,107 ,110 ,114 ,118 ,122 ,126 ,130 ,134 ,138 ,142 ,147 ,151 ,156 ,160 ,165 ,170 ,174 ,
179 ,184 ,189 ,194 ,200 ,205 ,210 ,216 ,221 ,227 ,232 ,238 ,244 ,250 ,256 ,262 ,268 ,274 ,280 ,287 ,293 ,
299 ,306 ,313 ,319 ,326 ,333 ,340 ,347 ,354 ,361 ,369 ,376 ,383 ,391 ,398 ,400 ,
#else

//z29
23  ,24  ,25  ,26  ,28  ,29  ,31  ,33  ,35  ,37  ,38  ,40  ,42  ,44  ,46  ,49  ,51  ,53  ,55  ,58  ,60  ,
62  ,65  ,67  ,70  ,73  ,75  ,78  ,81  ,84  ,86  ,89  ,92  ,95  ,98  ,101 ,105 ,108 ,111 ,114 ,118 ,121 ,
124 ,128 ,131 ,135 ,139 ,142 ,146 ,150 ,154 ,157 ,161 ,165 ,169 ,173 ,178 ,182 ,186 ,190 ,194 ,199 ,203 ,
208 ,212 ,217 ,221 ,226 ,230 ,235 ,240 ,245 ,250 ,255 ,260 ,265 ,270 ,275 ,280 ,285 ,290 ,296 ,301 ,306 ,
312 ,317 ,323 ,329 ,334 ,340 ,346 ,351 ,357 ,363 ,369 ,375 ,381 ,387 ,393 ,400 

#endif

};

const u16 cctPwmList[35][3] =
{
#if 0

	

#else
	{26,0   ,400},
	{27,35  ,365},
	{28,55  ,345},
	{29,70  ,330},
	{30,90  ,310},
	{31,105 ,295},
	{32,120 ,280},
	{33,135 ,265},
	{34,145 ,255},
	{35,160 ,240},
	{36,170 ,230},
	{37,180 ,220},
	{38,190 ,210},
	{39,200 ,200},
	{40,210 ,190},
	{41,220 ,180},
	{42,230 ,170},
	{43,235 ,165},
	{44,245 ,155},
	{45,255 ,145},
	{46,265 ,135},
	{47,275 ,125},
	{48,280 ,120},
	{49,290 ,110},
	{50,300 ,100},
	{51,305 ,95 },
	{52,315 ,85 },
	{53,320 ,80 },
	{54,330 ,70 },
	{55,335 ,65 },
	{56,345 ,55 },
	{57,350 ,50 },
	{58,360 ,40 },
	{59,365 ,35 },
	{60,400 ,0 },
#endif

};

u8 Get_cctPwmList(u16 _cct)
{
    u8 i;
	for(i=0;i<35;i++)
	{
		if(_cct/100 ==cctPwmList[i][0])
		{
			break;
		}
		
	}

return(i);
}



void Jisuan_cct(u8 m_dim,u16 m_cct, u16* w_cct,u16* y_cct)
{
	u8 cctId=0;
    cctId=Get_cctPwmList(m_cct);
	*w_cct = (Dim_tab[m_dim]*cctPwmList[cctId][1]/PWM_MAX);
	*y_cct =(Dim_tab[m_dim]*cctPwmList[cctId][2]/PWM_MAX);

}

void Dim_CCT_set()
{

#if (COLOR_SINGLE==COLOR_CATEGORY)
	//单色
	W_Pwm(Jisuan_dim() );
	Y_Pwm(Jisuan_dim() );

#elif (COLOR_DUAL==COLOR_CATEGORY)
	u8 cctId=0;
    cctId=Get_cctPwmList(sysdata.cct);
	W_Pwm(Dim_tab[sysdata.dim]*cctPwmList[cctId][1]/PWM_MAX);
	Y_Pwm(Dim_tab[sysdata.dim]*cctPwmList[cctId][2]/PWM_MAX);
	
	cctId=Get_cctPwmList(sysdata.Ctrl.cct1);
	pwm_w1=(Dim_tab[sysdata.Ctrl.dim1]*cctPwmList[cctId][1]/PWM_MAX);
	pwm_y1=(Dim_tab[sysdata.Ctrl.dim1]*cctPwmList[cctId][2]/PWM_MAX);
	
	cctId=Get_cctPwmList(sysdata.Ctrl.cct2);
	pwm_w2= (Dim_tab[sysdata.Ctrl.dim2]*cctPwmList[cctId][1]/PWM_MAX);
	pwm_y2= (Dim_tab[sysdata.Ctrl.dim2]*cctPwmList[cctId][2]/PWM_MAX);
	
	cctId=Get_cctPwmList(sysdata.Ctrl.cct3);
	pwm_w3= (Dim_tab[sysdata.Ctrl.dim3]*cctPwmList[cctId][1]/PWM_MAX);
	pwm_y3= (Dim_tab[sysdata.Ctrl.dim3]*cctPwmList[cctId][2]/PWM_MAX);
	
	cctId=Get_cctPwmList(sysdata.Ctrl.cct4);
	pwm_w4= (Dim_tab[sysdata.Ctrl.dim4]*cctPwmList[cctId][1]/PWM_MAX);
	pwm_y4= (Dim_tab[sysdata.Ctrl.dim4]*cctPwmList[cctId][2]/PWM_MAX);
	 

#endif

}



void eff_strobe(void)
{
    itimer++;

    u16 rate =(u16)(100-sysdata.eff_rate);
    if(3>rate)
        rate=3; 
    
    rate*=8;
    if((itimer <rate) ||(itimer ==rate))
    {
        Dim_CCT_set();

    }
    else
    {
        if(itimer >2*rate)
            itimer=0;
        LEC_AllOff();
        
    }

}
//效果，1ms调用周期
void LED_Effect()
{
    Effect_t eFFectMode=sysdata.eff_mode;

    if(sysdata.dim==0)
    {
		LEC_AllOff();
		return;
    }
	if(Event_GET(EVENT_EFFECT_INIT))
	{
		Event_CLR(EVENT_EFFECT_INIT);
		eff_InitTimer();
	}
    switch(eFFectMode)
    {
        case Effect_OFF:
        {
			Dim_CCT_set();
        }return;
        case Strobe:
        {
            
            eff_strobe();
            
        }return;
        case Paparazzi:
        {
			
            eff_paparazzi();
            

        }return;
        case Shortout:
        {
            eff_shortout();
        }return;
        case Fire:
        {
			 

			// pwm_w=5;
			u8 rate =100-sysdata.eff_rate;

			itimer++;
			if(itimer<rate/10)
				return;
			itimer=0;

			{
				eff_fireY();//黄
				// eff_fireY2();//黄可调亮度
				eff_fireW();//白
				// printf("d: %d,%d\n",printWpwm,printYpwm);
			}

			
        }return;
        default:return;
    }
	//	printf("sd: %d,%d\n",pwm_w,pwm_y);
}

//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
u16    eff_pa_table[20+6] =
{
    //  700,500,100,100,400,100,100,100,800,100,900,100,100,100,400,100,200,600,100,300

7,5,1,1,4,1,1,1,8,1,
9,1,1,1,4,1,2,6,1,3,
1,1,1,1,1,1

};
static u8   eff_n = 0;
void eff_paparazzi()
{
    
    Y_Pwm(0);
	pwm_y1=0;	
	pwm_y2=0;	
	pwm_y3=0;	
	pwm_y4=0;
	pwm_w1=0;	
	pwm_w2=0;	
	pwm_w3=0;	
	pwm_w4=0;

    u8 rate =100-sysdata.eff_rate;
    if(rate<1)
        rate=1;

	if(itimer++ <rate)
		return;
	itimer=0;

    
    if (eff_count== 1)
    {
#if (COLOR_SINGLE==COLOR_CATEGORY)
		Y_Pwm(Jisuan_dim()]);
#elif (COLOR_DUAL==COLOR_CATEGORY)
        Y_Pwm(0);
#endif
		// pwm_w=(Jisuan_dim(sysdata.dim));
		pwm_w=Dim_tab[sysdata.dim];

    }
    else if(eff_count== 2)
    {

		pwm_w=0;
        
    }
    else if(eff_count > eff_pa_table[randNo]+1)
    {
		eff_count=0;

		randNo=( (u8)(effr_m + (effr_n - effr_m) * randa( &effr_r)) );

    }
	eff_count++;


}


//-----------------------------------------------------------------------------------------------
u16 eff_shortout_table[15+20] =
{
/* 	700,500,100,100,400,100,100,100,800,
	100,900,100,100,100,400
 */
	70,
	50,
	10,
	10,
	40,
	10,
	10,
	10,
	80,
	10,
	90,
	10,
	10,
	10,
	40,
// ---------------
	26,
	95,
	46,
	55,
	76,
	52,
	37,
	34,
	33,
	59,
	83,
	82,
	83,
	70,
	63,
	30,
	76,
	66,
	15,
	53

};
void eff_shortout()
{
   	pwm_y=0;	
	pwm_y1=0;	
	pwm_y2=0;	
	pwm_y3=0;	
	pwm_y4=0;
	pwm_w1=0;	
	pwm_w2=0;	
	pwm_w3=0;	
	pwm_w4=0;

    u8 rate =100-sysdata.eff_rate;
    if(rate <1)
        rate=1;
	if(itimer++ <rate)
		return;
	itimer=0;

    eff_count++;
    if(eff_count == 1)
    {

        // Y_Pwm(0);
        W_Pwm(0);

    }
    if(eff_count== 2)
    {
#if (COLOR_SINGLE==COLOR_CATEGORY)
		Y_Pwm(Jisuan_dim());
#elif (COLOR_DUAL==COLOR_CATEGORY)
        Y_Pwm(0);
#endif
		// pwm_w = Jisuan_dim(sysdata.dim);
		pwm_w = Dim_tab[sysdata.dim];
	}
    else if(eff_count >= eff_shortout_table[randNo]/4)
    {
        eff_count=0;

        randNo = (u8)(eff_ss_min + (eff_ss_max - eff_ss_min) * randa( (float*)&eff_ss_r ) );
/* 		printf("%d\n",randNo);
		randNo++;
		if(randNo==15+20-1)randNo=0;
 */
    }
    if(eff_count>100)
    {
        eff_count=0;
    }

}


//----------------------------------------------------------------------------------------
static void eff_fireY(void)
{
	static u16 dimpwm=0;
	
	if(Fire_tab[eff_count]<Dim_tab[sysdata.dim])
		dimpwm= Fire_tab[eff_count];	
	else
		dimpwm = Dim_tab[sysdata.dim];

	
	if( Yeff_count < dimpwm)
		Yeff_count+=1;

	else if( Yeff_count == dimpwm )
		eff_count = (u8)(eff_Yfire_min + (eff_Yfire_max - eff_Yfire_min) * randa((float*)&eff_Yfire_r));

	else if( Yeff_count > dimpwm)
		Yeff_count-=1;
	
    Y_Pwm(Yeff_count);


}
static void eff_fireY2(void)
{
	static u16 dimpwm=0;
	
	if(Fire_tab[eff_count]<Dim_tab[sysdata.dim])
		dimpwm= Fire_tab[eff_count];	
	else
		dimpwm = Dim_tab[sysdata.dim];
	
	if( Yeff_count < dimpwm)
		Yeff_count+=1;
	else if( Yeff_count == dimpwm )
		eff_count++;
	else if( Yeff_count > dimpwm)
		Yeff_count-=1;

	if(eff_count==999)
		eff_count=0;

	Y_Pwm(Yeff_count);

}

//白色
static void eff_fireW(void)
{

	W_Cnt++;
	if(W_Cnt<20)
		return;
	W_Cnt=0;

#if 0
    if( Weff_count < Dim_tab[Weff_randn])
	{
		Weff_count+=1;
	}
	else if( Weff_count == Dim_tab[Weff_randn])
	{

		// eff_Wfire_max=5;
		Weff_randn = (u8)(eff_Wfire_min + (eff_Wfire_max - eff_Wfire_min) * randa(&eff_Wfire_r));

	}
	else if( Weff_count > Dim_tab[Weff_randn])
	{
		Weff_count-=1;
	}

#endif
	if(!Weff_randn)
	{
		if( Weff_count < eff_Wfire_max)
		{
			Weff_count+=1;
		}
		else
		{
			Weff_randn=1;
		}
	}
	else
	{

		if( Weff_count > eff_Wfire_min)
		{
			Weff_count-=1;
		}
		else Weff_randn=0;
	}

    W_Pwm(Weff_count);


}

//功能同步
void eff_InitTimer()
{
    itimer=0;
    eff_count=0;
    randNo=0;

    // effr_m = 1;
    // eff_n = 0;
    // effr_r = 19;

    // eff_ss_max = 8;
    // eff_ss_min = 0;
    // eff_ss_r = 8;

    // eff_Yfire_max = 100;
    // eff_Yfire_min = 0;
    // eff_Yfire_r = 8;

    // eff_Wfire_max = 5;
    // eff_Wfire_min = 2;
    // eff_Wfire_r = 1;

    W_Cnt=0;


    Yeff_flag=0; 
    Weff_randn=1; 


    HAL_DWTInit(0);

    Yeff_count=0;
    Weff_count=0;
    // Yeff_count=68;
    // Weff_count=18;

    // HAL_Delay(10);
}

void LEC_AllOff()
{
	pwm_y =0;
	pwm_w =0;
	// pwm_y1 =0;
	// pwm_w1 =0;
	// pwm_y2 =0;
	// pwm_w2 =0;
	// pwm_y3 =0;
	// pwm_w3 =0;
	// pwm_y4 =0;
	// pwm_w4 =0;

}